package chat.asynch;

import chat.message.ChatMessage;
import chat.message.ChatMessageManager;

import javax.servlet.AsyncContext;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AsyncRequestProcessor implements Runnable {
    private AsyncContext asyncContext;


    public AsyncRequestProcessor(AsyncContext asyncContext) {
        this.asyncContext = asyncContext;
    }


    public void run() {
        try {
            ChatMessage message = ChatMessageManager.getMessage();
            ServletResponse servletResponse = asyncContext.getResponse();
            servletResponse.setCharacterEncoding("UTF-8");
            servletResponse.getWriter().write(message.toJSON());
        } catch (IOException e) {
            e.printStackTrace();
        }
        asyncContext.complete();
    }
}
